package sii.processors;
import java.io.*;

public class SplitFileRatingToTest {
	
	
	
	public static void main(String[] args) throws Exception {
		LoadRatings();
	}

	private static void LoadRatings() throws Exception {
		String inputpath;
		String trainingpath;
		String testpath;
		String line;
		double testrate = 0.2;

		BufferedReader inputbuffer;
		BufferedWriter trainingbuffer;
		BufferedWriter testbuffer;

		File current_dir;
		current_dir = new File("");

		inputpath = current_dir.getAbsolutePath().toString()
				+ System.getProperty("file.separator") + "trainingRatings.dat";

		trainingpath = current_dir.getAbsolutePath().toString()
				+ System.getProperty("file.separator") + "trainig.dat";
		testpath = current_dir.getAbsolutePath().toString()
				+ System.getProperty("file.separator") + "test.dat";

		inputbuffer = new BufferedReader(new FileReader(inputpath));
		testbuffer = new BufferedWriter(new FileWriter(testpath));
		trainingbuffer = new BufferedWriter(new FileWriter(trainingpath));

		int i = 0;
		System.out.println("Splittamento in corso...");
		while ((line = inputbuffer.readLine()) != null) {
			if (i%1000==0)
				System.out.println(i + " righe caricate...");
			
			String[] params = line.split(",");
			Double random = Math.random();
			if (random>testrate){
				trainingbuffer.write(params[0]+","+params[1]+","+params[2]+"\n");
			}else{
				testbuffer.write(params[0]+","+params[1]+","+params[2]+"\n");
			}
			i++;
		}
		inputbuffer.close();
		testbuffer.close();
		trainingbuffer.close();
		System.out.println(i + " righe totali caricate!");
		System.out.println("Splittamento terminato.");
		
	}
}
